package com.example.shan307.myapplication;

import android.widget.Toast;
import com.baidu.mapapi.*;
import android.app.Application;
import android.preference.PreferenceManager;

/**
 * Created by SHAN307 on 2015/11/19.
 */
public class App extends Application {
    static App mDemoApp;

    // 百度MapAPI的管理类
    public BMapManager mBMapMan = null;

    // 授权Key
    // TODO: 请输入您的Key,
    // 申请地址：http://dev.baidu.com/wiki/static/imap/key/
    public String mStrKey = "E3041FEDFA4A24627A4B76539E07658B0FE44A5D";
    boolean m_bKeyRight = true; // 授权Key正确，验证通过

    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    public static class MyGeneralListener implements MKGeneralListener {
        @Override
        public void onGetNetworkState(int iError) {
//			Toast.makeText(App.mDemoApp.getApplicationContext(), "您的网络出错啦！",
//					Toast.LENGTH_LONG).show();
        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
                // 授权Key错误：
                Toast.makeText(App.mDemoApp.getApplicationContext(),
                        "请在BMapApiDemoApp.java文件输入正确的授权Key！", Toast.LENGTH_LONG)
                        .show();
                App.mDemoApp.m_bKeyRight = false;
            }
        }
    }

    @Override
    public void onCreate() {
        mDemoApp = this;
        mBMapMan = new BMapManager(this);
        mBMapMan.init(this.mStrKey, new MyGeneralListener());
        mBMapMan.getLocationManager().setNotifyInternal(10, 5);
        // if (mBMapMan != null) {
        // mBMapMan.destroy();
        // mBMapMan = null;
        // }

        super.onCreate();
    }

    @Override
    // 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
    public void onTerminate() {
        if (mBMapMan != null) {
            mBMapMan.destroy();
            mBMapMan = null;
        }
        super.onTerminate();
    }

    public void SaveBegin(String name, String begin) {
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
                .edit().putString(name, begin).commit();
    }

    public String getUSerid() {
        return PreferenceManager.getDefaultSharedPreferences(
                getApplicationContext()).getString("userid", "");
    }

    public void SaveLogin(boolean begin) {
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
                .edit().putBoolean("islogin", begin).commit();
    }

    public boolean getLogin() {
        return PreferenceManager.getDefaultSharedPreferences(
                getApplicationContext()).getBoolean("islogin", false);
    }

    public void ShowToast(int msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void ShowToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}
